$(function() {

	// layui表格
	layui.use(['table','form',"layer"], function() {
		var table = layui.table;
		var form = layui.form;
		var layer = layui.layer;
		var T = table.render({
			elem : '#DeptTable',
			page : true,
			limit:5,
			limits:[5,10,15],
			url : '/selectDept' // 数据接口
			,
			response : {
				statusCode : 200
			// 重新规定成功的状态码为 200，table 组件默认为 0
			},
			parseData : function(res) { // res 即为原始返回的数据
				
				return {
					"count" : res.tol,
					"code" : res.status, // 解析接口状态
					"msg" : res.message, // 解析提示文本
					"data" : res.list
				// 解析数据列表
				};
			},
				done:function(res,curr){
					console.log("res:"+res);
					console.log("curr:"+curr);
					currs=curr;
				},
			
			cols : [ [ // 表头
			{
				field : 'deptId',
				title : '院系编号',
				width : 200,
				align:'center',
				sort : true,
				fixed : 'left'
			}, {
				field : 'deptName',
				align:'center',
				title : '院系名称'
			}, {
				field : 'operate',
				align:'center',
				toolbar : '#barDemo',
				title : '操作',
				width : 180
			} ] ]
			
			
			
			
				 
		});
		 
		// 监听编辑按钮
		table.on('tool(DeptTable)',function(obj){
			var data = obj.data;
			
			console.log(data);
			if(obj.event=='edit'){
				layer
				.open({
					type : 1,
					skin : 'layui-layer-rim', // 加上边框
					area : [ '520px', '300px' ], // 宽高
					content : '<form class="layui-form layui-form-pane" action="#" id="form1">\n'
							+ '  <br>\n'
							+ '  <div class="layui-form-item">\n'
							+ '    <label class="layui-form-label">院系编号</label>\n'
							+ '    <div class="layui-input-block">\n'
							+ '      <input type="text" id="deptId" lay-verify="title" autocomplete="off" name="deptId"  readonly disabled class="layui-input form-control">\n'
							+ '    </div>\n'
							+ '  </div>\n'
							+ '  <div class="layui-form-item">\n'
							+ '    <label class="layui-form-label">院系名称</label>\n'
							+ '    <div class="layui-input-block">\n'
							+ '      <input type="text" id="deptName" name="deptName" lay-verify="title" autocomplete="off" placeholder="不能为空" class="layui-input">\n'
							+ '    </div>\n'
							+ '  </div>\n'
							+ '<div ><button lay-submit lay-filter="btn"  class="layui-btn" style="margin-left: 160px;" >提交</button></div>\n'
							+ '</form>'
				});
				form.render();
				var deptId=data.deptId;
				var deptName=data.deptName;
				console.log(deptId);
				console.log(deptName)
				$("#deptId").val(deptId);
				$("#deptName").val(deptName);
			}
			
			
		})
		
			form.on('submit(btn)', function(data){
					var formData = data.field;
					var deptId = formData.deptId;
					var deptName = formData.deptName;
					
					
//				var deptId=$("#deptId").val();
//				var deptName=$("#deptName").val();
				console.log(deptId);
				console.log(deptName);
				if(deptName!=""){
				console.log("deptName:"+deptName);
					$.ajax({
						url : "/updateDept",
						type : "post",
						data : {
							'deptId' : deptId,
							'deptName' : deptName
						},
						dataType : "json",
						success : function() {
							layer.closeAll();
//							T.reload();
							layer.msg("修改成功");
							
						},
						error : function() {
							layer.msg("error");
						}
					})
				}else{
					layer.msg("请输入院系名字");
				}
				return false;
			})
		

		form.on('submit(formmus)',function(){
			
			var deptId = $("#deptId").val();
			var deptName =$("#deptName").val();
			console.log(deptId)
			
			$.ajax({
				url:"/insertDept",
				type:"post",
				data:{deptId:deptId,deptName:deptName},
				dataType:"json",
				success:function(d){
					
					if(d.msg=="YES"){
						
						layer.msg("增添成功");
						
						
						table.reload('DeptTable',{
							
							where:{
								deptId:deptId
							},
							page:{
								curr:1
							}
						});
						
					}
					
					
				},
				error:function(){
					layer.msg("失败,id只能是数字,Id不能重复或不能为空");
					
				}
			})
			
			return false;
		})				
				
		
		
		
	});

	// 添加院系
	$("#Add_Department")
			.on(
					"click",
					function() {
						layer
								.open({
									type : 1,
									skin : 'layui-layer-rim', // 加上边框
									area : [ '520px', '300px' ], // 宽高
									content : '<form class="layui-form layui-form-pane" action="" id="form1">\n'
											+ '  <br>\n'
											+ '  <div class="layui-form-item">\n'
											+ '    <label class="layui-form-label">院系编号</label>\n'
											+ '    <div class="layui-input-block">\n'
											+ '      <input type="text" id="deptId" lay-verify="title" autocomplete="off" placeholder="工号不能相同" class="layui-input" >\n'
											+ '    </div>\n'
											+ '  </div>\n'
											+ '  <div class="layui-form-item">\n'
											+ '    <label class="layui-form-label">院系名称</label>\n'
											+ '    <div class="layui-input-block">\n'
											+ '      <input type="text" id="deptName" lay-verify="title" autocomplete="off" placeholder="不能为空" class="layui-input">\n'
											+ '    </div>\n'
											+ '  </div>\n'
											+ '<div ><button lay-submit lay-filter="formmus" class="layui-btn" style="margin-left: 160px;">提交</button>\n'
											+ '<button type="reset" class="layui-btn layui-btn-primary">取消</button></div>\n'
											+ '</form>'
								});
					})
					
			
					
})